/*
 *      Created on: Mar 8, 2011
 *      Author: topcoder
 */
#include <Volume.h>
#include <VolumeUnit.h>

namespace
{
    const VolumeUnit volumeUnit[3] = {OZ, TBSP, TSP};
    const unsigned int MAX_NUM_OF_VOLUME_UNIT = sizeof(volumeUnit)/sizeof(Unit);
}

Volume::Volume(const unsigned int amount, const VolumeUnit& volumeUnit)
:Quantity(amount, volumeUnit)
{
}

Volume Volume::operator+(const Volume& volume)const
{
    return Volume(amountInBaseUnit_m+volume.amountInBaseUnit_m, VOLUME_BASE_UNIT);
}

void Volume::PrintVolume(ostream& os)const
{
    unsigned int i = 0,amount = amountInBaseUnit_m;

    for(i = 0; i < MAX_NUM_OF_VOLUME_UNIT; i++) 
    {            
	if(amount >= (volumeUnit[i].getFactor()))
        {
           os<< amount/(volumeUnit[i].getFactor()) << ' ' << volumeUnit[i] << ' ';              
        }
        amount = amount%(volumeUnit[i].getFactor());
    }
}

void Volume::PrintInFormat1(ostream& os)const
{  
    if(amountInBaseUnit_m == 0)
    {
        os<< amountInBaseUnit_m << ' ' << unit_m<< ' ';
    }
    else 
    {
        PrintVolume(os);
    }
}

void Volume::PrintInFormat2(ostream& os)const
{    
    os<< amountInBaseUnit_m << ' ' << VOLUME_BASE_UNIT;
}


